package org.xq.softcup.mq.admin.service.impl;

import org.springframework.stereotype.Service;
import org.xq.softcup.mq.admin.model.MqBiz;
import org.xq.softcup.mq.admin.util.JsonResult;
import org.xq.softcup.mq.admin.dao.IMqBizDao;
import org.xq.softcup.mq.admin.dao.IMqTopicDao;
import org.xq.softcup.mq.admin.service.IMqBizService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author admin
 */
@Service
public class MqBizServiceImpl implements IMqBizService {

    @Resource
    private IMqBizDao mqBizDao;
    @Resource
    private IMqTopicDao mqTopicDao;


    @Override
    public List<MqBiz> findAll() {
        return mqBizDao.findAll();
    }

    @Override
    public MqBiz load(int id) {
        return mqBizDao.load(id);
    }

    @Override
    public JsonResult<String> add(MqBiz mqBiz) {
        // exist
        List<MqBiz> list = findAll();
        if (list != null) {
            for (MqBiz item: list) {
                if (item.getBizName().equals(mqBiz.getBizName())) {
                    return new JsonResult<>(JsonResult.FAIL_CODE, "业务线名称不可重复");
                }
            }
        }

        int ret = mqBizDao.add(mqBiz);
        return ret>0?JsonResult.SUCCESS:JsonResult.FAIL;
    }

    @Override
    public JsonResult<String> update(MqBiz mqBiz) {
        // exist
        List<MqBiz> list = findAll();
        if (list != null) {
            for (MqBiz item: list) {
                if (item.getId()!=mqBiz.getId() && item.getBizName().equals(mqBiz.getBizName())) {
                    return new JsonResult<>(JsonResult.FAIL_CODE, "业务线名称不可重复");
                }
            }
        }
        int ret = mqBizDao.update(mqBiz);
        return ret>0?JsonResult.SUCCESS:JsonResult.FAIL;
    }

    @Override
    public JsonResult<String> delete(int id) {
        int count = mqTopicDao.pageListCount(0, 1, id, null);
        if (count > 0) {
            return new JsonResult<>(JsonResult.FAIL_CODE, "禁止删除，该业务线已使用");
        }
        int ret = mqBizDao.delete(id);
        return ret>0?JsonResult.SUCCESS:JsonResult.FAIL;
    }

}
